package com.b1.controller;

import cn.hutool.core.bean.BeanUtil;
import com.b1.common.BaseResponse;
import com.b1.common.DeleteRequest;
import com.b1.common.ErrorCode;
import com.b1.common.ResultUtils;
import com.b1.exception.BusinessException;
import com.b1.exception.ThrowUtils;
import com.b1.pojo.dto.PartolSleepRecord.PartolSleepRecordAddRequest;

import com.b1.pojo.dto.partolSleepRecord.PartolSleepRecordQueryRequest;
import com.b1.pojo.enity.PartolSleepRecord;
import com.b1.service.PartolSleepRecordService;
import com.b1.service.TeacherSleepRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("b1/PartolSleepQuery")
@CrossOrigin
@Api(tags = "幼儿园记录睡眠表")
@Slf4j
public class PartolSleepRecordController {
    @Resource
    private PartolSleepRecordService partolSleepRecordService;
    @PostMapping("/add")
    @ApiOperation(value = "添加记录")
    public BaseResponse<Integer> addChildernSleepRecord(@RequestBody PartolSleepRecordAddRequest partolSleepRecordAddRequest){
        if (partolSleepRecordAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        PartolSleepRecord partolSleepRecord = new PartolSleepRecord();

        BeanUtil.copyProperties(partolSleepRecordAddRequest, partolSleepRecord);
        //childernSleepRecordService.validChildernSleepRecord(childernSleepRecord, true);
        //partolSleepRecord.setCheckTime(new Date());
        boolean result = partolSleepRecordService.save(partolSleepRecord);
        ThrowUtils.throwIf(!result ,ErrorCode.OPERATION_ERROR, "添加失败");
        Integer recordId = partolSleepRecord.getId();
        return ResultUtils.success(recordId);
    }
    @PostMapping("/delete")
    @ApiOperation(value = "删除记录")
    public BaseResponse<Boolean> deleteChildernSleepRecord(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long id = deleteRequest.getId();
        // 判断是否存在
        PartolSleepRecord byId = partolSleepRecordService.getById(id);
        ThrowUtils.throwIf(byId == null, ErrorCode.NOT_FOUND_ERROR);
        boolean b = partolSleepRecordService.removeById(id);
        return ResultUtils.success(b);
    }
    @PostMapping("/update")
    @ApiOperation(value = "更新午睡记录")
    public BaseResponse<Boolean> updatePost(@RequestBody  PartolSleepRecord partolSleepRecord) {
        if (partolSleepRecord == null || partolSleepRecord.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 参数校验
       // childernSleepRecordService.validChildernSleepRecord(childernSleepRecord, false);
        long id = partolSleepRecord.getId();
        // 判断是否存在
        PartolSleepRecord byId = partolSleepRecordService.getById(id);
        ThrowUtils.throwIf(byId == null, ErrorCode.NOT_FOUND_ERROR);

        boolean result = partolSleepRecordService.updateById(partolSleepRecord);
        return ResultUtils.success(result);
    }
    @PostMapping("/list/page")
    @ApiOperation(value = "分页获取午睡记录")
    public BaseResponse<Page<PartolSleepRecord>> listPostByPage(@RequestBody PartolSleepRecordQueryRequest partolSleepRecordQueryRequest) {
        long current = partolSleepRecordQueryRequest.getCurrent();
        long size = partolSleepRecordQueryRequest.getPageSize();
        QueryWrapper<PartolSleepRecord> queryWrapper = partolSleepRecordService.getQueryWrapper(partolSleepRecordQueryRequest);
        Page<PartolSleepRecord> page = partolSleepRecordService.page(new Page<>(current, size),queryWrapper);
        return ResultUtils.success(page);
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出午睡记录")
    public BaseResponse<String> export(@RequestBody Long[] ids) throws IOException {

        return ResultUtils.success(partolSleepRecordService.export(ids));
    }

    @PostMapping("/updateBatch")
    @ApiOperation(value = "批量更新记录")
    public BaseResponse<Boolean> updateBatch(@RequestBody List<PartolSleepRecord> partolSleepRecordList) {
        if (partolSleepRecordList == null || partolSleepRecordList.isEmpty()) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "更新记录不能为空");
        }
        for (PartolSleepRecord partolSleepRecord : partolSleepRecordList) {
            if (partolSleepRecord.getId() <= 0) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "更新记录id不能为空");
            }
            // 判断是否存在
            PartolSleepRecord byId = partolSleepRecordService.getById(partolSleepRecord.getId());
            ThrowUtils.throwIf(byId == null, ErrorCode.NOT_FOUND_ERROR, "更新记录不存在");
        }
        return ResultUtils.success(partolSleepRecordService.updateBatchById(partolSleepRecordList));
}
}